package coupon

import (
	"context"

	"mall/common/globalkey"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/promotion/model"
	"mall/service/promotion/rpc/internal/svc"
	"mall/service/promotion/rpc/internal/utils/errorhandler"
	"mall/service/promotion/rpc/types/promotion"

	"github.com/zeromicro/go-zero/core/logx"
)

type AttachProductToCouponLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAttachProductToCouponLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AttachProductToCouponLogic {
	return &AttachProductToCouponLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AttachProductToCouponLogic) AttachProductToCoupon(in *promotion.CouponTemplateProductInfo) (*promotion.BaseResp, error) {
	if in.CouponId == nil || *in.CouponId == 0 {
		return nil, errorx.NewInvalidArgumentError("common.validationError")
	}

	if (in.ProductId == nil || *in.ProductId == 0) && (in.BrandId == nil || *in.BrandId == 0) && (in.CategoryId == nil || *in.CategoryId == 0) {
		return nil, errorx.NewInvalidArgumentError("common.validationError")
	}

	filterMap := make(map[string]map[string][]any)
	filterMap[model.CouponProductRelationshipColumns.CouponID] = map[string][]any{
		"equalTo": {*in.CouponId},
	}
	var productId uint64
	if in.ProductId != nil && *in.ProductId > 0 {
		productId = *in.ProductId
		filterMap[model.CouponProductRelationshipColumns.ProductID] = map[string][]any{
			"equalTo": {productId},
		}
	}
	var brandId uint64
	if in.BrandId != nil && *in.BrandId > 0 {
		brandId = *in.BrandId
		filterMap[model.CouponProductRelationshipColumns.BrandID] = map[string][]any{
			"equalTo": {brandId},
		}
	}
	var categoryId uint64
	if in.CategoryId != nil && *in.CategoryId > 0 {
		categoryId = *in.CategoryId
		filterMap[model.CouponProductRelationshipColumns.CategoryID] = map[string][]any{
			"equalTo": {categoryId},
		}
	}
	var productType uint8
	if in.ProductType != nil && *in.ProductType > 0 {
		productType = uint8(*in.ProductType)
		filterMap[model.CouponProductRelationshipColumns.ProductType] = map[string][]any{
			"equalTo": {productType},
		}
	}
	list, _, err := l.svcCtx.CouponTemplate.GetRelProductList(l.ctx, model.M{
		"pageSize": uint64(1),
	}, -1, filterMap)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	var rel *model.CouponProductRelationship
	if len(list) > 0 {
		rel = list[0]
		if in.Sort != nil {
			rel.Sort = uint16(*in.Sort)
		}
	} else {
		var sort uint16
		if in.Sort != nil {
			sort = uint16(*in.Sort)
		} else {
			sort = uint16(globalkey.SystemDefaultSort)
		}

		rel = &model.CouponProductRelationship{
			CouponID:    *in.CouponId,
			BrandID:     brandId,
			CategoryID:  categoryId,
			ProductID:   productId,
			ProductType: productType,
			Sort:        sort,
		}
	}

	err = l.svcCtx.CouponTemplate.AttachProductToCoupon(l.ctx, nil, rel)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	return &promotion.BaseResp{
		Msg: i18n.CreateSuccess,
	}, nil
}
